//
//package tcg.serialization;
//
//import java.util.concurrent.ConcurrentMap;
//import java.util.concurrent.locks.Lock;
//import java.util.concurrent.locks.ReentrantLock;
//
//import de.justphil.tcg.tcgserver.commons.config.GridConfig;
//import de.justphil.tcg.tcgserver.commons.grid.Grid;
//import de.justphil.tcg.tcgserver.game.mechanics.example.DeckCreator;
//import de.justphil.tcg.tcgserver.game.mechanics.game.Game;
//import de.justphil.tcg.tcgserver.game.mechanics.game.OnGameProgressListener;
//import de.justphil.tcg.tcgserver.game.mechanics.gui.Board;
//import de.justphil.tcg.tcgserver.game.mechanics.monster.MonsterList;
//import de.justphil.tcg.tcgserver.game.mechanics.util.tools.simulation.AnotherUserSimulator;
//import org.slf4j.Logger;
//
//import com.hazelcast.core.Hazelcast;
//import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.*;
//
//public class AnotherGameSimulationTest implements OnGameProgressListener {
//	
//	private static final Logger				log						=
//																			gameLogger(GameSimulationTest.class);
//	private static final int				RUNS					= 100;
//	private static final int				START_SEED				= 1;
//	// private static final int POLL_INTERVAL = 0;
//	private static final int				WAIT_BETWEEN_GAMES_MS	= 1000;
//	/* -1 for no automatic selection */
//	private static int						SELECTION_TIME_MS		= 0;
//	/* -1 for no automatic confirmation */
//	private static int						CONFIRM_TIME_MS			= 0;
//	private static final boolean			USE_SERIALIZATION		= true;
//	private final boolean					USE_GUI					= WAIT_BETWEEN_GAMES_MS >= 100;
//	private final boolean					USE_SIMULATOR			= SELECTION_TIME_MS >= 0
//																			|| CONFIRM_TIME_MS >= 0;
//	private ConcurrentMap<Integer, Game>	gamesMap;
//	private volatile boolean				gameOver;
//	private volatile boolean				selectionRequired;
//	private volatile boolean				confirmRequired;
//	private volatile int					selectionPlayer;
//	private AnotherUserSimulator			simulator;
//	private Game							game;
//	private Board							board;
//	private final Lock						lock;
//	
//	public AnotherGameSimulationTest() {
//	
//		game = null;
//		board = null;
//		simulator = null;
//		lock = new ReentrantLock();
//	}
//	
//	public static void main(String[] args) {
//	
//		AnotherGameSimulationTest test = new AnotherGameSimulationTest();
//		test.simulation();
//		System.out.println("MAIN THREAD END.");
//	}
//	
//	public void simulation() {
//	
//		if (USE_SERIALIZATION) {
//			initSerialization();
//		}
//		// creation of Game object
//		for (int seed = START_SEED; seed < RUNS + START_SEED; seed++) {
//			DeckCreator deckCreator = new DeckCreator();
//			MonsterList[] decks = new MonsterList[] {
//					deckCreator.getRandomDeck(seed), deckCreator.getRandomDeck(Long.MAX_VALUE - seed) };
//			game = new Game(seed, decks);
//			game.addOnGameProgressListener(this);
//			if (USE_GUI) {
//				startGUI();
//			}
//			if (USE_SIMULATOR) {
//				startSimulator();
//			}
//			if (USE_SERIALIZATION) {
//				serialize();
//				deserialize();
//			}
//			lock.lock();
//			startGame(seed);
//			lock.unlock();
//			while (!gameOver) {
//				// Thread.sleep(POLL_INTERVAL);
//				// Thread.sleep(1);
//				if (selectionRequired) {
//					handleSelectionRequired();
//				}
//				else if (confirmRequired) {
//					handleConfirmRequired();
//				}
//			}
//			handleGameOver();
//			try {
//				simulator.join();
//			}
//			catch (InterruptedException ignored) {
//			}
//			/*try { //Thread.sleep(WAIT_BETWEEN_GAMES_MS); } catch (InterruptedException e) { } */
//		}
//		if (USE_SERIALIZATION) {
//			finishSerialization();
//		}
//	}
//	
//	@Override
//	public void onGameOver(int winner) {
//	
//		gameOver = true;
//	}
//	
//	@Override
//	public void onProgress(int player, int slot, long cardId, int[] propertyTypes, int life,
//			int attack, int defense, int agility) {
//	
//		if (USE_SIMULATOR) {
//			simulator.onProgress(player, slot, cardId, propertyTypes, life, attack, defense,
//					agility);
//		}
//	}
//	
//	@Override
//	public void onGameInfo(int type, String text) {
//	
//	}
//	
//	@Override
//	public void onSelectionRequired(int player, int slot) {
//	
//		selectionPlayer = player;
//		selectionRequired = true;
//	}
//	
//	@Override
//	public void onResumeRequired(int player) {
//	
//		confirmRequired = true;
//	}
//	
//	private void handleSelectionRequired() {
//	
//		selectionRequired = false;
//		if (USE_SERIALIZATION) {
//			lock.lock();
//			serialize();
//			deserialize();
//			lock.unlock();
//		}
//		if (USE_SIMULATOR) {
//			simulator.onSelectionRequired(selectionPlayer, -1);
//		}
//	}
//	
//	private void handleConfirmRequired() {
//	
//		confirmRequired = false;
//		if (USE_SERIALIZATION) {
//			lock.lock();
//			serialize();
//			deserialize();
//			lock.unlock();
//		}
//		if (USE_SIMULATOR) {
//			simulator.onResumeRequired(-1);
//		}
//	}
//	
//	private void handleGameOver() {
//	
//		gameOver = false;
//		if (USE_SIMULATOR) {
//			simulator.onGameOver(-1);
//		}
//		log.info("# Game Over.", new Object[] {});
//	}
//	
//	private void initSerialization() {
//	
//		Grid.init();
//		gamesMap = Hazelcast.getMap(GridConfig.GRID_TEST_GAMES);
//	}
//	
//	private void finishSerialization() {
//	
//		gamesMap = null;
//		Grid.shutdownAll();
//	}
//	
//	private void startGame(int seed) {
//	
//		log.info("# Starting Game.", new Object[] {});
//		game.onResume();
//	}
//	
//	private void startSimulator() {
//	
//		log.info("# Starting UserSimulalator with selection time {} and confirm time {}.",
//				new Object[] {
//						SELECTION_TIME_MS, CONFIRM_TIME_MS });
//		simulator = new AnotherUserSimulator(START_SEED, SELECTION_TIME_MS, CONFIRM_TIME_MS, lock);
//		simulator.setOnUserInteractionListener(game);
//		simulator.start();
//	}
//	
//	private void startGUI() {
//	
//		/* Killing the previously shown GUI */
//		if (board != null) {
//			board.exit();
//		}
//		log.info("# Starting GUI.", new Object[] {});
//		board = new Board(game);
//		game.addOnGameProgressListener(board);
//		board.addOnUserInteractionListener(game);
//	}
//	
//	private void serialize() {
//	
//		log.info("# Serializing.", new Object[] {});
//		gamesMap.put(1, game);
//		if (board != null) {
//			board.removeOnUserInteractionListener(game);
//		}
//		if (simulator != null) {
//			simulator.setOnUserInteractionListener(null);
//		}
//		game = null;
//	}
//	
//	private void deserialize() {
//	
//		log.info("# Deserializing.", new Object[] {});
//		game = gamesMap.get(1);
//		game.addOnGameProgressListener(this);
//		if (board != null) {
//			game.addOnGameProgressListener(board);
//			board.addOnUserInteractionListener(game);
//		}
//		if (simulator != null) {
//			simulator.setOnUserInteractionListener(game);
//		}
//	}
//}
